/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.main;

import com.ttt.nor2.config.Config;
import com.ttt.nor2.thread.ThImportBilling;
import com.ttt.nor2.util.ChkArgs;
import com.ttt.nor2.util.ExceptionUtil;
import java.io.File;
import java.util.Hashtable;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Administrator
 */
public class MainImportDB {

    static Logger log = Logger.getLogger(MainImportDB.class);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //System.out.println(System.getProperty("user.dir"));
        /*
        String cl = "aaaaxxxxx_13456.txt";
        System.out.println(cl.substring(0, cl.indexOf("_")));
        System.exit(-1);
        */
        ChkArgs chkargs = new ChkArgs(args);
        Properties prop = chkargs.getProps();

        PropertyConfigurator.configure(chkargs.getFileConfig());
        log.info("Start Application import data...");
        String url = prop.getProperty("URL");
        String usr = prop.getProperty("USER");
        String pwd = prop.getProperty("PWD");
        String db = prop.getProperty("DBNAME");
        String classname = "";// get value from file config
        //String chSplit = prop.getProperty("CHAR_SPLIT");//get value from file config
        String []strClass = prop.getProperty("CLASS_VALUE").split("@@");
        Hashtable hmClass = new Hashtable();
        for(int i = 0; i < strClass.length; i++){
            String [] cNameValue = strClass[i].split("=");
            hmClass.put(cNameValue[0], cNameValue[1]);
            log.info("class["+cNameValue[0]+"]split["+cNameValue[1]+"]");
        }

        log.info((String)hmClass.get("Import4CS"));
        //System.exit(-1);

        String pathU = prop.getProperty("PATH_UPLOAD");// get value from pathfile
        String pathP = prop.getProperty("PATH_PROCESS");
        String pathS = prop.getProperty("PATH_SUCCESS");
        int sleep = Integer.parseInt(prop.getProperty("THRED_SLEEP"));
        int thWork = Integer.parseInt(prop.getProperty("THREAD_WORK"));
        

        while (true) {
            // list file in diretory upload
            File pUpload = new File(pathU);

            String farr[] = pUpload.list();
            //Connection conn = null;
            try {

                // for loop
                for (int i = 0; i < farr.length; i++) {

                    //conn = DBMysql.dbConn(url, db, usr, pwd);

                    //check working thread
                    if (ThImportBilling.getThWork() > thWork) {
                        thSleep(sleep);
                        continue;
                    }
                    //move file working process
                    File sf = new File(pathU + farr[i]);
                    File df = new File(pathP + farr[i]);
                    sf.renameTo(df);
                    log.info("Move file  :[" + pathU + farr[i] + "] =>[" + pathP + farr[i] + "]");
                    classname = farr[i].substring(0, farr[i].indexOf("_"));
                    log.info("Load config classname[" + classname + "]chsplit[" + (String)hmClass.get(classname) + "]paht[" + pathU + "]");
                    // config for class importBilling
                    Config cfg = new Config();
                    //cfg.setChSplit(chSplit);
                    cfg.setChSplit((String)hmClass.get(classname));
                    cfg.setClassname(classname);
                    cfg.setFileprop(chkargs.getFileConfig());
                    cfg.setFilename(farr[i]);
                    cfg.setPathProcess(pathP);
                    cfg.setPathSuccess(pathS);
                    cfg.setUrl(url);
                    cfg.setUsr(usr);
                    cfg.setPwd(pwd);
                    cfg.setDb(db);
                    //run thread service
                    ThImportBilling th = new ThImportBilling(cfg);
                    th.start();
                    //th.run();
                    //th.join();

                }//end for
                System.gc();
            } catch (Exception e) {
                log.error("Exception " + ExceptionUtil.getStackTrace(e));
            }
            // thread sleep
            thSleep(sleep);

        }//end loop while never die
    }// end main

    private static void thSleep(int sleep) {
        try {
            Thread.sleep(sleep);
            log.debug("Thread sleep [" + sleep + "]");
        } catch (Exception e) {
            log.error("Exception Thread sleep " + ExceptionUtil.getStackTrace(e));
        }
    }
}
